package leetcode19;

import domain.ListNode;

import java.util.ArrayList;

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode index1 = head;
        ListNode index2 = head;
        ListNode pre = head;
        for(int i = 0;i<n;i++){
            index1 = index1.next;
        }
        for(int i = 0;index1!=null;i++){
            index1 = index1.next;
            index2 = index2.next;
            if(i!=0){
                pre = pre.next;
            }
        }
        ListNode node = index2;
        ListNode next = node.next;
        if(pre!=node){
            pre.next = next;
        }else{
            head = next;
        }
        return head;
    }
}